package cn.vshare.controller;

import cn.vshare.entity.PermissionFirst;
import cn.vshare.service.PermissionFirstService;
import com.github.pagehelper.Page;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.*;

@RestController(value = "permissionFirstController")
public class PermissionFirstController {
    @Resource
    private PermissionFirstService permissionFirstService;

    //http://localhost:8080/getAllPermissionFirst.do
    @RequestMapping(value = "getAllPermissionFirst",method = RequestMethod.POST)
    public List<PermissionFirst> getAllPermissionFirst(){
        Map<String,Object>map=new HashMap<>();
        map.put("is_forbidden",0);
        List<PermissionFirst>permissionFirstList=permissionFirstService.queryByConditions1(map);
        System.out.println(permissionFirstList);
        return permissionFirstList;
    }

    @RequestMapping(value = "getPermissionFirstById",method = RequestMethod.POST)
    public String getPermissionFirstById(String id2PermissionFirst,
                                          HttpSession session,
                                          HttpServletRequest request,
                                          HttpServletResponse response) throws ServletException, IOException {
        String result="false";
        //System.out.println("id2PermissionFirst====="+id2PermissionFirst);
        PermissionFirst permissionFirst=permissionFirstService.queryById(id2PermissionFirst);
        session.setAttribute("permissionFirst",permissionFirst);//存储到Session域中
        //response.sendRedirect("/h-ui/admin-permission-first-edit.jsp");//转发
        if (permissionFirst!=null){
            result ="true";
        }else {
            result="flase";
        }
        return result;
    }


    @RequestMapping(value = "queryPermissionFirstById",method = RequestMethod.POST)
    public PermissionFirst queryPermissionFirstById(String idFirst){
        System.out.println("idFirst===="+idFirst);
        PermissionFirst permissionFirst=permissionFirstService.queryById(idFirst);
        return permissionFirst;
    }

    @RequestMapping(value = "getAllPermissionFirstByPage")
    public Map<String,Object> getAllPermissionFirstByPage(int pageNum,int pageSize,String name2First){

        System.out.println("name2First======"+name2First);
        Map<String,Object>map=new HashMap<>();
        map.put("pageNum",pageNum);//当前页码
        map.put("pageSize",pageSize);//每页显示的最大记录数
        map.put("name",name2First);//二级权限名称的关键字
        Page page =permissionFirstService.queryByConditions2(map);
        map.put("page",page);//list集合
        map.put("totalRecords",page.getTotal());//总记录数
        map.put("totalPages",page.getPages());//总页数
        return map;
    }


    @RequestMapping(value = "addPermissionFirst",method = RequestMethod.POST)
    public String addPermissionFirst(String permissionFirstName){
        System.out.println(permissionFirstName);
        //新增一级权限
        PermissionFirst permissionFirst=new PermissionFirst();
        permissionFirst.setId(UUID.randomUUID().toString());
        permissionFirst.setName(permissionFirstName);
        int row= permissionFirstService.addPermissionFirst(permissionFirst);
        if (row==1){
            System.out.println("新增成功");
            return "true";
        }else {
            System.out.println("新增失败");
            return "false";
        }
    }


    @RequestMapping(value = "updatePermissionFirst")
    public String updatePermissionFirst(PermissionFirst permissionFirst){
        System.out.println("getId====="+permissionFirst.getId());
        System.out.println("getName====="+permissionFirst.getName());
        System.out.println("getIsForbidden====="+permissionFirst.getIsForbidden());

        int row=permissionFirstService.updatePermissionFirst(permissionFirst);

        if (row==1){
            System.out.println("更新成功");
            return "true";
        }else {
            System.out.println("更新失败");
            return "false";
        }
    }

    @RequestMapping(value = "deletePermissionFirst",method = RequestMethod.POST)
    public String deletePermissionFirst(String delete2PermissionFirstId){
        System.out.println("delete2PermissionFirstId===="+delete2PermissionFirstId);

        int row=permissionFirstService.deletePermissionFirstById(delete2PermissionFirstId);

        if (row==1){
            System.out.println("删除成功");
            return "true";
        }else {
            System.out.println("删除失败");
            return "false";
        }
    }


    //批量删除
    @RequestMapping(value = "delete2Array2PermissionFirst",method = RequestMethod.POST)
    public String delete2Array2PermissionFirst(@RequestBody List<String> delete2Array2PermissionFirstId){
        System.out.println("delete2Array2PermissionFirstId===="+delete2Array2PermissionFirstId);

        List<Integer>list=new ArrayList<>();
        String result="false";
        for (int i=0;i<delete2Array2PermissionFirstId.size();i++){
            int row=permissionFirstService.deletePermissionFirstById(delete2Array2PermissionFirstId.get(i));
            list.add(row);
        }

        //判断是否全部删除,如果删除异常
        //如果删除异常,应该使用事物回滚,需要完善
        for (int i=0;i<list.size();i++){
            if (list.get(i)==1){
                System.out.println("删除成功");
                result="true";
            }else {
                System.out.println("删除失败");
                result="false";
                break;
            }
        }
        return result;

    }


    @RequestMapping(value = "toAdmin_permission_first_edit")
    public ModelAndView toAdmin_permission_first_edit(){
        return new ModelAndView("h-ui/admin_permission_first_edit");
    }
   

}
